<template>
    <div class="shop-item-page">
        <!-- 图片 -->
        <img :src="item.img">  
        <!-- 标题 -->
        <div class="title-box">
            <!-- 左侧文字 -->
            <div class="text">
                <p>{{item.name}}</p>
                <p>{{ item.french }}</p>
            </div>
            <!-- 右侧购物车图标 -->
            <img @click.stop="handleClick" src="../assets/gouwuche.png">
        </div>
        <!-- 价格 -->
        <div class="price">
            <span>￥</span>
            <span>{{item.price}}</span>
        </div>
    </div>
</template>

<script>
export default {
    props: ["item"],
    name: 'ShopItem',
    components: {},
    data() {
        return {}
    },
    created() {},
    methods: {
        handleClick() {
            this.addShopCart();
            this.goDetail();
        },
        goDetail() {
            this.$router.push({
                name: "detail",
                params: this.item
            });
        },
        addShopCart() {
            this.item.num = 1;
            this.item.checked = false;
            let data = localStorage.getItem("shopCartData") ? JSON.parse(localStorage.getItem("shopCartData")) : [];
            data = [...data, this.item];
            localStorage.setItem("shopCartData", JSON.stringify(data));
            console.log('商品已添加到购物车', this.item);
        }
    }
}
</script>

<style scoped lang='less'>
.shop-item-page {
    width: calc(50% - 5px); /* calc()计算不同单位的宽度 运算符两端要用空格 */
    margin-bottom: 16px;
    &:nth-child(2n-1) {
        margin-right: 10px;
    }
    & img:first-child {
        width: 100%;
        height: 176px;
    }
    .title-box {
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin: 12px 0;
        .text p:first-child {
            font-size: 14px;
            font-weight: 600;
        }
        .text p:last-child {
            font-size: 10px;
            color: #999;
        }
        img {
            width: 30px;
            height: 30px;
            cursor: pointer; /* 确保鼠标悬停时显示为手型 */
        }
    }
    .price span {
        font-weight: 600;
    }
    .price span:first-child {
        font-size: 8px;
    }
    .price span:last-child {
        font-size: 12px;
    }
}
</style>